const serveStatic = require('serve-static');
const bodyParser = require('body-parser')
const multipart = require('connect-multiparty');
const path = require('path');
const api = require('./api')
module.exports.beforeApp = function(app){
    //请求参数转换
    const urlencoded = bodyParser.urlencoded({ extended: false })
    const jsonParser = bodyParser.json()
    const rawParser = bodyParser.raw()
    app.use(multipart({}))
    app.use(function (req, res, next) {
        if (req.url.indexOf('/mock') < 0) {
            return next()
        }
        urlencoded(req, res, (err) => {
            if (err) return next(err)
            jsonParser(req, res, (err) => {
                if (err) return next(err)
                rawParser(req, res, next)
            })
        })
    })
    //pages
    app.use('/dev/code-gen', serveStatic(path.join(__dirname, '../dist')))

    // 服务
    api.installApp(app)
}
